#include <QDebug>
#include "simpletcpserverdll.h"

SimpleTcpServerDLL::SimpleTcpServerDLL(){
    int loop_i = 0;
    this->TcpServerNumber = 0;
    for(loop_i=0;loop_i<maxSERVERNUMBER;loop_i++){
        pTcpServer[loop_i] = new QTcpServer();
        pSimpleTcpSeverInfo[loop_i] = new SimpleTcpSeverInfo;

        pSimpleTcpSeverInfo[loop_i]->Port = 0;
        pSimpleTcpSeverInfo[loop_i]->TcpClinetNumber = 0;
        pSimpleTcpSeverInfo[loop_i]->switch_add = false;
        pSimpleTcpSeverInfo[loop_i]->mark_used = false;
        pSimpleTcpSeverInfo[loop_i]->switch_listen = false;
        pSimpleTcpSeverInfo[loop_i]->mark_listendone = false;
    }
}

SimpleTcpServerDLL::~SimpleTcpServerDLL(){
    int loop_i = 0;
    for(loop_i=0;loop_i<maxSERVERNUMBER;loop_i++){
        if(pTcpServer[loop_i] != nullptr){
            delete pTcpServer[loop_i];
        }
        if( pSimpleTcpSeverInfo[loop_i] != nullptr){
            delete pSimpleTcpSeverInfo[loop_i];
        }
    }
}

short SimpleTcpServerDLL::SimpleServeradd(const int port, int& order){
    if(TcpServerNumber < maxSERVERNUMBER){
        order = this->TcpServerNumber;
        pSimpleTcpSeverInfo[TcpServerNumber]->Port = port;
        pSimpleTcpSeverInfo[TcpServerNumber]->TcpClinetNumber = 0;
        pSimpleTcpSeverInfo[TcpServerNumber]->switch_add = true;
        this->TcpServerNumber = this->TcpServerNumber + 1;
        return 1;
    }
    return 0;
}

void  SimpleTcpServerDLL::SimpleServerMainThreadLoopRun(short type){
    switch (type) {
        case 1:
            this->SimpleServerMainThreadLoopRun_StateChange();
            break;
        case 2:
            this->SimpleServerMainThreadLoopRun_Send();
            break;
        case 3:
            this->SimpleServerMainThreadLoopRun_Receive();
            break;
        default:
            break;
    }
}

short SimpleTcpServerDLL::SimpleTcpServerConnetedClientNumber(const int order,int& number){
    number =  pSimpleTcpSeverInfo[order]->TcpClinetNumber;
    return 1;
}

short SimpleTcpServerDLL::SimpleHasNewClientConnect(int order){
    if(this->pSimpleTcpSeverInfo[order]->TcpClinetNumber < maxSERVERNUMBER){
        this->pSimpleTcpSeverInfo[order]->pTcpSocket[pSimpleTcpSeverInfo[order]->TcpClinetNumber] = new QTcpSocket();
        this->pSimpleTcpSeverInfo[order]->pTcpSocket[pSimpleTcpSeverInfo[order]->TcpClinetNumber] = this->pTcpServer[order]->nextPendingConnection();
        this->pSimpleTcpSeverInfo[order]->pSimpleTcpClientInfo[pSimpleTcpSeverInfo[order]->TcpClinetNumber]->mark_connect = true;
        this->pSimpleTcpSeverInfo[order]->TcpClinetNumber = this->pSimpleTcpSeverInfo[order]->TcpClinetNumber + 1;
        return 1;
    }
    return 0;
}

short SimpleTcpServerDLL::SimpleServerlisten(const int order){
    pSimpleTcpSeverInfo[order]->switch_listen = true;
    pSimpleTcpSeverInfo[order]->mark_listendone = false;
    return 1;
}

short SimpleTcpServerDLL::SimpleTcpServerWriteData(const int Order, const int subOrder, QByteArray Data){
    if(pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_sendbusy == false){
        pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->SendData = Data;
        pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->switch_send = true;
        pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_sendbusy = true;
        return 1;
    }
    return 0;
}

short SimpleTcpServerDLL::SimpleTcpServerReadData(const int Order, const int subOrder, QByteArray& Data){
    if(this->pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_ready2read == true){
        Data = pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->RecvData;
        this->pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_ready2read = false;
        return 1;
    }
    return 0;
}

void SimpleTcpServerDLL::SimpleServerMainThreadLoopRun_StateChange(){
    int loop_i = 0;
    int loop_j = 0;

    for(loop_i = 0; loop_i < TcpServerNumber; loop_i++){ //考虑效率，只需要扫描已经添加的服务器即可,注意这里是小于，非小于等于！！
        if(pSimpleTcpSeverInfo[loop_i]->switch_add == true){//二次开发中是否初次添加了服务器
            for(loop_j = 0; loop_j < maxSERVERNUMBER ; loop_j++){
                this->pSimpleTcpSeverInfo[loop_i]->pTcpSocket[loop_j] = nullptr;
                this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j] = new SimpleTcpClientInfo;

                this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->switch_send = false;
                this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->switch_receive = false;
                this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_connect = false;
                this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_sendbusy = false;
                this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_ready2read = false;
            }
            pSimpleTcpSeverInfo[loop_i]->switch_add = false;
            pSimpleTcpSeverInfo[loop_i]->mark_used = true;
        }

        if(pSimpleTcpSeverInfo[loop_i]->mark_used == true){//是否服务器正在使用
            if(pSimpleTcpSeverInfo[loop_i]->switch_listen == true && pSimpleTcpSeverInfo[loop_i]->mark_listendone == false){//是否监听中
                this->pTcpServer[loop_i]->listen(QHostAddress::Any,pSimpleTcpSeverInfo[loop_i]->Port);
                this->pSimpleTcpSeverInfo[loop_i]->switch_listen = false;
                this->pSimpleTcpSeverInfo[loop_i]->mark_listendone = true;
            }
            else if(pSimpleTcpSeverInfo[loop_i]->switch_listen == false && pSimpleTcpSeverInfo[loop_i]->mark_listendone == true){
                if(this->pTcpServer[loop_i]->waitForNewConnection(1)){//该服务有新的客户端连接
                    this->SimpleHasNewClientConnect(loop_i);
                }
            }
        }
    }
}

void SimpleTcpServerDLL::SimpleServerMainThreadLoopRun_Send(){
    int loop_i = 0;
    int loop_j = 0;
    for(loop_i = 0; loop_i < TcpServerNumber; loop_i++){
        if(pSimpleTcpSeverInfo[loop_i]->mark_used == true){
            for(loop_j = 0; loop_j < pSimpleTcpSeverInfo[loop_i]->TcpClinetNumber; loop_j++){//考虑效率，只需要扫描已经添加的客户端即可,注意这里是小于，非小于等于！！
                if(pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_connect == true){
                    if(this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->switch_send == true){//是否有需要发送的消息
                        this->pSimpleTcpSeverInfo[loop_i]->pTcpSocket[loop_j]->write(pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->SendData);
                        this->pSimpleTcpSeverInfo[loop_i]->pTcpSocket[loop_j]->waitForBytesWritten(1);
                        this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->switch_send = false;
                        this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_sendbusy = false;
                    }

                }
            }
        }
    }
}

void SimpleTcpServerDLL::SimpleServerMainThreadLoopRun_Receive(){
    int loop_i = 0;
    int loop_j = 0;
    for(loop_i = 0; loop_i < TcpServerNumber; loop_i++){
        if(pSimpleTcpSeverInfo[loop_i]->mark_used == true){
            for(loop_j = 0; loop_j < pSimpleTcpSeverInfo[loop_i]->TcpClinetNumber; loop_j++){//考虑效率，只需要扫描已经添加的客户端即可,注意这里是小于，非小于等于！！
                if(pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_connect == true){
                    if(pSimpleTcpSeverInfo[loop_i]->pTcpSocket[loop_j]->waitForReadyRead(1) == true){//是否有需要读取的消息
                        this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->RecvData = this->pSimpleTcpSeverInfo[loop_i]->pTcpSocket[loop_j]->readAll();
                        this->pSimpleTcpSeverInfo[loop_i]->pSimpleTcpClientInfo[loop_j]->mark_ready2read = true;
                    }
                }
            }
        }
    }
}

bool  SimpleTcpServerDLL::TcpServerSendBusy(const int Order,const int subOrder){
    bool temp = (this->pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_connect);
    if(temp == true){
        return  this->pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_sendbusy;
    }
    return false;
}

bool  SimpleTcpServerDLL::TcpServerRead2Read(const int Order,const int subOrder){
    bool temp = (this->pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_connect);
    if(temp == true){
        return  this->pSimpleTcpSeverInfo[Order]->pSimpleTcpClientInfo[subOrder]->mark_ready2read;
    }
    return false;
}

